package kr.re.etri.snsenabler.client.auth;

import kr.re.etri.snsenabler.client.i18n.I18NConstants;
import kr.re.etri.snsenabler.client.service.OAuthServiceAsync;
import kr.re.etri.snsenabler.shared.Data;
import kr.re.etri.snsenabler.shared.ServiceResponse;
import kr.re.etri.snsenabler.shared.SocialServiceProvider;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextField;

abstract class OAuthAuthorizationDialog extends AbstractAuthorizationDialog {

	private static OAuthConnectionDialogUiBinder uiBinder = GWT.create(OAuthConnectionDialogUiBinder.class);

	interface OAuthConnectionDialogUiBinder extends UiBinder<Widget, OAuthAuthorizationDialog> {
	}

	private Dialog dialog;
	@UiField Label descriptionLabel;
	@UiField TextButton authorizeButton;
	@UiField TextField verifierTextField;
	@UiField TextButton sendVerifierButton;
	private String authorizationUrl;

	protected OAuthAuthorizationDialog(Data loginData, SocialServiceProvider socialServiceProvider) {
		super(loginData, socialServiceProvider);

		dialog = (Dialog)uiBinder.createAndBindUi(this);
		dialog.setHeadingText(I18NConstants.C.connectionDialogTitle() + " - " + socialServiceProvider.getTitle());

		descriptionLabel.setText("Authorization Description");//I18NMessages.M.authorizationDescription());
		authorizeButton.setText("Authorize");

		initialize();
	}

	protected abstract OAuthServiceAsync getService();

	@Override
	public Dialog getDialog() {
		return dialog;
	}

	private void initialize() {
		getService().fetchAuthorizationUrl(socialServiceProvider, new AsyncCallback<ServiceResponse>() {
			public void onSuccess(ServiceResponse result) {
				if (result.isFailed()) {
					AlertMessageBox messageBox = new AlertMessageBox("Alert", "Invalid Authorization");
					messageBox.show();
				} else {
					String newAuthorizationUrl = result.getData().getString(Data.KEY_1);
					GWT.log("authorizationUrl: " + newAuthorizationUrl);
					authorizationUrl = newAuthorizationUrl;
				}
			}

			public void onFailure(Throwable caught) {
				GWT.log("authorize: " + caught.getMessage());
				dialog.hide();
			}
		});
	}

	@UiHandler("authorizeButton")
	public void onAuthorizeButtonClicked(SelectEvent event) {
		Window.open(authorizationUrl, "Authorization", "");
		GWT.log("Window opened");
	}

	@UiHandler("sendVerifierButton")
	public void onSendVerifierButtonClicked(SelectEvent event) {
		getService().sendVerifier(loginData, socialServiceProvider, verifierTextField.getText().trim(), new AsyncCallback<ServiceResponse>() {
			public void onSuccess(ServiceResponse result) {
				GWT.log("result: " + result.getData());
				socialServiceData = result.getData();
				dialog.hide();
			}

			public void onFailure(Throwable caught) {
				GWT.log("sendVerifier: " + caught.getMessage());
				dialog.hide();
			}
		});
	}
}
